//
//  AccountSecurityViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "AccountSecurityViewController.h"
#import "AccountSecurityTableViewCell.h"
#import "AmendPhoneViewController.h"
#import "MailboxBindingViewController.h"
#import "ModifyPasswordViewController.h"
#import "LandingRecordViewController.h"

static NSString *const AccountSecurityTableViewCell_ID = @"AccountSecurityTableViewCell_ID";

@interface AccountSecurityViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;//tableView
@property (nonatomic, strong) UIButton *nextButton;

@end

@implementation AccountSecurityViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账户安全";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupConstraints];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake(20, 50, SCREEN_WIDTH-40, 40);
    self.nextButton.backgroundColor = UIColorFromRGBHex(0xfa7a09);
    self.nextButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular];
    self.nextButton.layer.cornerRadius = 5.0f;
    self.nextButton.layer.masksToBounds = YES;
    
    [self.nextButton setTitle:@"退出登录" forState:UIControlStateNormal];

    WEAK_SELF
    [[self.nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        STRONG_SELF
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kAccessToken];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"User_Id"];
        self.tabBarController.selectedIndex = 0;
        [self.navigationController popToRootViewControllerAnimated:YES];

    }];
    [self.view addSubview:self.nextButton];
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.bottom.offset(-30);
        make.height.mas_equalTo(40);
    }];

}

- (void)setupConstraints {
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[AccountSecurityTableViewCell class] forCellReuseIdentifier:AccountSecurityTableViewCell_ID];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

#pragma mark -
#pragma mark tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *titlesArr = @[@"绑定手机",@"登录邮箱",@"登录密码",@"登录记录",@"敏感操作"];
    NSArray *placeArr = @[@"修改/解绑",@"绑定/解绑",@"修改",@" ", @" "];
    AccountSecurityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AccountSecurityTableViewCell_ID forIndexPath:indexPath];
    cell.stateL.text = titlesArr[indexPath.row];
    cell.prompt.text = placeArr[indexPath.row];
    return cell;
}

#pragma mark tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 5)];
    footView.backgroundColor = kRGB(245, 245, 245, 1);
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 5)];
    footView.backgroundColor = kRGB(245, 245, 245, 1);
    return footView;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        AmendPhoneViewController *apVC = [[AmendPhoneViewController alloc] init];
        [self.navigationController pushViewController:apVC animated:YES];
    } else if (indexPath.row == 1) {
        MailboxBindingViewController *mbVC = [[MailboxBindingViewController alloc] init];
        [self.navigationController pushViewController:mbVC animated:YES];
    } else if (indexPath.row == 2) {
        ModifyPasswordViewController *mpVC = [[ModifyPasswordViewController alloc] init];
        [self.navigationController pushViewController:mpVC animated:YES];
    } else if (indexPath.row == 3) {
        LandingRecordViewController *lrVC = [[LandingRecordViewController alloc] init];
        [self.navigationController pushViewController:lrVC animated:YES];
    } else {
        LandingRecordViewController *lrVC = [[LandingRecordViewController alloc] init];
        lrVC.titleStr = @"1";
        [self.navigationController pushViewController:lrVC animated:YES];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
